<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Model_Allocate
 *
 * @author Schorr
 */
class Model_Allocate extends Model_Base {
      public function allocate_create($data)
    {
	$link_table = "person_link_person";
	$this->db->insert($link_table, $data);
    }
    
    public function findAllocation($idperson1,$idperson2)    
    {
	
	$select = $this->db->select()
		->from(array("person_link_person"),array ('id'))
		->Where ('idperson1 = ?',$idperson1)
		->Where ('idperson2 = ?',$idperson2)
		->orWhere ('idperson1 = ?',$idperson2)
		->Where ('idperson2 = ?',$idperson1);
	return $this->db->fetchAll($select);;
    }
    
    public function allocate_delete($id)
    {
	$where = $this->db->quoteInto('id = ?', $id);
	$link_table = "person_link_person";
	$this->db->delete($link_table, $where);
    }
    
     public function allocate_deleteByPersonIdAction($id)
     {
	 $select = $this->db->select()
		->from(array("person_link_person"),array ('id'))
		->Where ('idperson1 = ?' ,$id)
		->orWhere ('idperson2 = ?' ,$id);
	 
	 $plp_id = $this->db->fetchAll($select);	 
	 foreach ($plp_id as $value) {
	     $m = new Model_Allocate;
	     $m->allocate_delete($value['id']);
	}	    
	 return $this->db->fetchAll($select);
     }
     
    public function listALLeventsByPersonID($id,$old){
	
	if ($old == 0){
	$select = $this->db->select()
	    ->from (array('elp' => 'event_link_person'),array ('idevent' => 'idevent'))
	    ->joinInner (array('e'=>'event'), 'elp.idevent = e.id')    
	    ->where('elp.idperson = ?', $id)
	    ->where ('e.date_from >= ?', date('Y-m-d'))
	    ->order ('e.date_from');}
	    else{
	$select = $this->db->select()
	    ->from (array('elp' => 'event_link_person'),array ('idevent' => 'idevent'))
	    ->joinInner (array('e'=>'event'), 'elp.idevent = e.id')    
	    ->where('elp.idperson = ?', $id)
	    ->where ('e.date_from < ?', date('Y-m-d'))
	    ->order ('e.date_from');}
	return $this->db->fetchAll($select);

	
    }
}

?>
